﻿@using DCMS.Web.Extensions;
@using DCMS.Web.Framework;
@using DCMS.Web.Framework.UI;
@using DCMS.ViewModel.Models.WareHouses;
@model InventoryProfitLossBillModel


<script type="text/javascript">

    jQuery(document).ready(function () {

        "use strict";

        // Init Theme Core
        Core.init();

        // Init Demo JS
        Demo.init();
        var model = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model));
        if (model.ReversedStatus) {
            $("#inkpadBox").hide();
            $("#inkpadBox1").css('display', 'block');
        }

        //商品列表选择赋值
        window.operateEventsPop = {
            'click .rowSelect': function (e, value, row, index) {
                try {
                    var index = $(this).attr("data-id");
                    var wareHouseId = $("#WareHouseId").val();

                    var keys = Object.keys(row.Units);
                    var inventoryByMinUnit = $("#InventoryByMinUnit").val();
                    //用户选择的价格体系
                    var defaultAmountId = 0;
                    //初始最小单位单位
                    var unitId = row.SmallUnitId;
                    var unitName = keys[0];
                    //如果不是最小单位
                    if (inventoryByMinUnit != "1" && inventoryByMinUnit != "True") {
                        //有大单位赋值大单位
                        if (row.BigUnitId != null && row.BigUnitId != 0) {
                            unitId = row.BigUnitId;
                            unitName = keys[2];
                        } else {
                            //有中单位赋值中单位
                            if (row.StrokeUnitId != null && row.StrokeUnitId != 0) {
                                unitId = row.StrokeUnitId;
                                unitName = keys[1];
                            }
                        }
                    }

                    //成本价
                    var costPrice = 0;
                    var ck = Object.keys(row.CostPrices);
                    var cv = Object.values(row.CostPrices);
                    ck.forEach(function (a, b) {
                        if (a == unitId) {
                            costPrice = cv[b];
                        }
                    });

                    //库存数量
                    var stockQty = 0;
                    //仓库Id
                    var wareHouseId = $("#WareHouseId").val();
                    row.StockQuantities.forEach(function (a, b) {
                        if (a.WareHouseId == wareHouseId) {
                            stockQty = a.UsableQuantity;
                        }
                    });
                    debugger;
                    $("#InventoryProfitLossItems").bootstrapTable('updateRow', {
                        index: index, row: {
                            ProductSKU: row.Sku,
                            ProductId: row.Id,
                            ProductName: row.Name,
                            BarCode: row.SmallBarCode,
                            UnitConversion: row.UnitConversion,
                            Units: row.Units,
                            UnitId: unitId,
                            UnitName: unitName,
                            Amount: 0,
                            Prices: row.Prices,
                            CostPrices: row.CostPrices,
                            CostPrice: costPrice,
                            CostAmount: 0,
                            ProductTierPrices: row.ProductTierPrices,  //价格体系
                            StockQty: stockQty,
                            StockQuantities: row.StockQuantities, //库存数量
                            BigQuantity: row.BigQuantity,//大转小
                            StrokeQuantity: row.StrokeQuantity,//中转小
                            SmallUnitId: row.SmallUnitId,  //小单位
                            StrokeUnitId: row.StrokeUnitId,  //中单位
                            BigUnitId: row.BigUnitId  //大单位
                        }
                    });
                    $('#ModalProductWindows').modal('hide');

                }
                catch (err) {
                    console.log(err);
                    $(this).showToastr(err);
                    return;
                }
            }
        };

        //项目
        $("#InventoryProfitLossItems").bootstrapTable({
                @Html.Raw(Model.Items.Count > 0 ? "url: '/InventoryProfitLoss/AsyncInventoryProfitLossItems?billId=" + Model.Id + "'," : "length: 9,")
            striped: true,
            cache: false,
            pagination: true,
            showPagination: false,
            sidePagination: "server",
            sortable: false,
            sortOrder: "asc",
            search: false,
            strictSearch: false,
            showColumns: false,
            showRefresh: false,
            clickToSelect: false,
            uniqueId: "id",
            showToggle: false,
            editable: true,
            cardView: false,
            detailView: false,
            showFooter: true,
            onLoadSuccess: function (data) {
                $(".fixed-table-pagination").hide();
            },
            onPostBody: function (data) {
                $("[data-toggle='popover']").popover();
            },
            columns: [
                {
                    title: '<i class="fa fa-gear fs18"></i>',
                    width: '50',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return m_pagerow + index + 1;
                    },
                    footerFormatter: function (value) {
                        return '总计';
                    }
                },
                { field: 'UnitId', visible: false },
                { field: 'Units', visible: false },
                { field: 'Prices', visible: false },
                { field: 'ProductTierPrices', visible: false },  //价格体系
                { field: 'SmallUnitId', visible: false }, //小单位
                { field: 'StrokeUnitId', visible: false }, //中单位
                { field: 'BigUnitId', visible: false }, //大单位
                { field: 'ProductId', visible: false },
                {
                    field: 'ProductSKU',
                    title: '商品编号',
                    width: '110',
                    formatter: $(this).formatIsNull
                },
                {
                    field: 'ProductName',
                    title: '商品名称',
                    width: '400',
                    formatter: function (value) {
                        return (value == 0 || value == "" || value == undefined) ? "" : @Model.Id== 0 ? $(this).cutString(value, 50) : "<a style='color:black; text-decoration:none;'  data-trigger='hover' title='' data-container='body' data-toggle='popover' data-placement='top' data-content='" + value + "'>" + $(this).cutString(value, 50) + "</a>";
                    }
                },
                {
                    field: 'BarCode',
                    title: '条形码',
                    width: '110',
                    formatter: $(this).formatIsNull
                },
                {
                    field: 'UnitName',
                    title: '单位',
                    formatter: $(this).formatIsNull
                },
                {
                    field: 'UnitConversion',
                    title: '单位换算',
                    formatter: $(this).formatIsNull
                },
                {
                    field: 'Quantity',
                    title: '数量',
                    formatter: $(this).formatIsNull
                },
                { field: 'CostPrices', visible: false },
                {
                    field: 'CostPrice',
                    title: '成本价',
                    formatter: $(this).formatFixed
                },
                {
                    field: 'CostAmount',
                    title: '成本金额',
                    formatter: $(this).formatFixed,
                    footerFormatter: function (value) {
                        var count = 0;
                        var datas = $('#InventoryProfitLossItems').bootstrapTable('getData');
                        var len = datas.length,
                            i, row;
                        //console.log(len);
                        if (len > 1) {
                            for (i = len - 1; i >= 0; i--) {
                                row = datas[i];
                                count += parseFloat((typeof (row.CostAmount) == 'undefined' || row.CostAmount == "") ? '0' : row.CostAmount);
                            }
                        }
                        else {
                            for (var i in value) {
                                count += parseFloat((typeof (value[i].CostAmount) == 'undefined' || value[i].CostAmount == "") ? '0' : value[i].CostAmount);
                            }
                        }
                        var total = parseFloat((typeof (count) == 'undefined' || count == "") ? '0' : count).toFixed(2);
                        return total;
                    }
                },
                {
                    field: 'StockQty',
                    title: '库存数量',
                    align: 'center',
                    width: '100',
                    //formatter: $(this).formatUnitName
                    formatter: function (value, row, index) {

                        if (value != "" && row.ProductId != "" && row.ProductId > 0) {

                            var bigUnitId = row.BigUnitId;
                            var bigUnitName = "";

                            var strokeUnitId = row.StrokeUnitId;
                            var strokeUnitName = "";

                            var smallUnitId = row.SmallUnitId;
                            var smallUnitName = "";

                            $.each(row.Units, function (key, unit) {
                                if (unit == bigUnitId) {
                                    bigUnitName = key;
                                }
                                else if (unit == strokeUnitId) {
                                    strokeUnitName = key;
                                }
                                else if (unit == smallUnitId) {
                                    smallUnitName = key;
                                }
                            });

                            //计算结果数量
                            var resultQuantity = value;
                            //大单位计算结果
                            var big = parseInt(resultQuantity / row.BigQuantity);
                            resultQuantity = resultQuantity - big * row.BigQuantity;
                            //中单位计算结果
                            var stroke = 0;
                            if (strokeUnitId > 0 && row.StrokeQuantity > 0) {
                                stroke = parseInt(resultQuantity / row.StrokeQuantity);
                                resultQuantity = resultQuantity - stroke * row.StrokeQuantity;
                            }
                            //小单位计算结果
                            var small = resultQuantity;

                            var content = big + "" + bigUnitName;

                            if (stroke > 0)
                                content += ((strokeUnitId > 0) ? (stroke + "" + strokeUnitName) : "");

                            if (small > 0)
                                content += small + "" + smallUnitName

                            return "<span alt=" + value + ">" + content + "</span>";
                        }
                        else {
                            return "";
                        }
                    }
                },
                {
                    field: 'Operate',
                    title: '操作',
                    align: 'center',
                    width: '120',
                    events: {
                        'click .rowAppend': function (e, value, row, index) {// 添加
                            //审核、红冲后不能修改
                            if ("@Model.AuditedStatus" == "True" || "@Model.ReversedStatus" == "True") {
                                return false;
                            }
                            $('#InventoryProfitLossItems').bootstrapTable('insert', row);
                        },
                        'click .rowCopy': function (e, value, row, index) {// 复制
                            //审核、红冲后不能修改
                            if ("@Model.AuditedStatus" == "True" || "@Model.ReversedStatus" == "True") {
                                return false;
                            }
                            $('#InventoryProfitLossItems').bootstrapTable('copying', row);
                            //修改最后一条数据uniqueid,防止复制时uniqueid相同，删除时都删除
                            var allTableData = $('#InventoryProfitLossItems').bootstrapTable('getData');
                            var maxuniqueid = 0;
                            $.each(allTableData, function (i, row) {
                                if (row.uniqueid > maxuniqueid) {
                                    maxuniqueid = row.uniqueid;
                                }
                            });
                            allTableData[allTableData.length - 1].uniqueid = maxuniqueid + 1;
                        },
                        'click .rowDel': function (e, value, row, index) {// 删除
                            //审核、红冲后不能修改
                            if ("@Model.AuditedStatus" == "True" || "@Model.ReversedStatus" == "True") {
                                return false;
                            }
                            var datas = $('#InventoryProfitLossItems').bootstrapTable('getData');
                            var len = datas.length;
                            if (len == 1) {
                                $(this).showToastr('数据不能删除');
                                return;
                            }

                            if (row.uniqueid == undefined) {
                                $("#InventoryProfitLossItems").bootstrapTable('remove', { field: "id", values: [parseInt(row.Id)] });
                            }
                            else {
                                $("#InventoryProfitLossItems").bootstrapTable('remove', { field: "uniqueid", values: [parseInt(row.uniqueid)] });
                            }
                        }
                    },
                    formatter: function (value, row, index) {
                        return [
                            '<button type="button" class="btn btn-sm btn-default rowAppend" title="添加"><i class="fa fa-plus" ></i></button>',
                            '<button type="button" class="btn btn-sm btn-default rowCopy" title="复制"><i class="fa fa-copy" ></i></button>',
                            '<button type="button" class="btn btn-sm btn-default rowDel" title="删除"><i class="fa fa-trash-o"></i></button>'
                        ].join('');
                    }
                }
            ],
            onClickCell: function (field, value, row, $element) {
                if (field == "Operate") {
                    return false;
                }

                var input = $($element).find("input");
                var select = $($element).find("select");
                if (input.length > 0 || select.length > 0)
                    return false;
                //当前列所在行的索引
                var index = $element.parent().attr("data-index");

                //审核、红冲后不能修改
                if ("@Model.AuditedStatus" == "True" || "@Model.ReversedStatus" == "True") {
                    return false;
                }

                //验证无效不允许修改编辑
                if (!formValidator()) {
                    return false;
                }

                if (field == "ProductName") {
                    var html = '<div class="input-group">' +
                        '<input type="text" class="form-control input-sm"  name="ProductName" value="' + (typeof (value) == 'undefined' ? "" : value) + '" id="' + field + "_" + row.Id + '">' +
                        '<span class="input-group-btn"><button type="button" class="btn btn-default btn-sm searchproduct" id="search_' + field + "_" + row.Id + '" data-index="' + index + '"><span class="glyphicon glyphicon-search"></span></button></span>' +
                        '</div>';
                    $($element).html(html);
                    var inputObj = $("#search_" + field + "_" + row.Id);
                    inputObj.focus();
                    inputObj.select();
                    inputObj.bind("focusout", function () {
                        $("#CostContractItems").bootstrapTable('updateRow', { index: index, row: { ProductName: $(this).val() } });
                        $($element).html("" + value + "");
                    });
                    //选择商品
                    inputObj.bind("click", function () {

                        var wareHouseId = $("#WareHouseId").val();
                        $(this).productSelectShowModal($("#ModalProductWindows"), "/Product/AsyncSearchSelectPopup?wareHouseId=" + wareHouseId + "&&stockQtyMoreThan=false&includeProductDetail=0", { Index: $(this).attr("data-index") }, "选择商品", $("#ProductModalForm"),
                            function () { });
                    });
                }
                else if (["Quantity"].indexOf(field) >= 0) {
                    //var isSmallUnit = $("#InventoryByMinUnit").val()
                    //if (isSmallUnit == "False") {
                    //    if (row.SmallUnitId == row.UnitId) {
                    //        $(this).showToastr('非以最小单位盘点，请选择非最小单位');
                    //        return false;
                    //    }
                    //} else {
                    //    if (row.SmallUnitId != row.UnitId) {
                    //        $(this).showToastr('以最小单位盘点，请选择最小单位');
                    //        return false;
                    //    }
                    //}
                    var html = '<input type="text" class="form-control input-sm" value="' + (typeof (value) == 'undefined' ? "0" : value) + '" id="' + field + "_" + row.Id + '">';
                    $($element).html(html);
                    var inputObj = $("#" + $(html).attr("id"));
                    inputObj.focus();
                    inputObj.select();
                    inputObj.bind("focusout", function () {
                        if (!isNaN($(this).val())) {
                            row[field] = $(this).val();
                            console.info("index:" + index);
                            var tradePrice = (row.CostPrice == null || row.CostPrice == "") ? 0 : row.CostPrice;
                            var costAmount = parseFloat(tradePrice) * parseFloat($(this).val());
                            costAmount = isNaN(costAmount) ? 0 : costAmount;
                            console.info(row["UnitName"]);
                            $("#InventoryProfitLossItems").bootstrapTable('updateRow', { index: index, row: { CostAmount: parseFloat(costAmount) } });
                            $($element).html("" + value + "");
                            console.info(row["UnitName"]);
                        }
                        else {
                            $(this).showToastr("只能输入数字!");
                            return false;
                        }
                    });
                }
                else if (field == "UnitName") {
                    var oldUnitName = row.UnitName;
                    if (row.Units == null) {
                        $(this).showToastr('请选择商品');
                        return;
                    }
                    let unitId = row.UnitId;

                    var html = '<select name="' + field + '" id="' + field + "_" + row.Id + '">';
                    //var keys = Object.keys(row.Units);
                    $.each(row.Units, function (key, unit) {
                        //html += '<option value="' + unit + '"  ' + (row.UnitId == unit ? "selected" : "") + '>' + key + '</option>';
                        if (unit > 0) {
                            html += '<option value="' + unit + '"  ' + (unitId == unit ? "selected" : "") + '>' + key + '</option>';
                        }
                    });
                    html += '</select>';

                    $($element).html(html);

                    var inputObj = $("#" + $(html).attr("id"));
                    inputObj.focus();
                    inputObj.select();
                    inputObj.bind("focusout", function () {
                        $($element).html("" + oldUnitName + "");
                    });
                    //单位选择
                    inputObj.bind("change", function () {

                        //console.log($(this).val());
                        //console.log($(this).find("option:selected").text());
                        //console.log(row.Prices);
                        var curUnit = parseInt($(this).val());
                        var isSmallUnit = $("#InventoryByMinUnit").val()
                        if (isSmallUnit == "False") {
                            if (row.SmallUnitId == curUnit) {
                                $(this).showToastr('非以最小单位盘点，请选择非最小单位');
                                return false;
                            }
                        } else {
                            if (row.SmallUnitId != curUnit) {
                                $(this).showToastr('以最小单位盘点，请选择最小单位');
                                return false;
                            }
                        }
                        oldUnitName = $(this).find("option:selected").text();
                        var tradePrice = 0;
                        $.each(row.Prices, function (i, price) {
                            if (curUnit == price.UnitId) {
                                tradePrice = price.ProductPrice != null ? price.ProductPrice.CostPrice : 0;
                            }
                        });

                        //成本价
                        var costPrice = 0;
                        //成本金额
                        var costAmount = 0;
                        var ck = Object.keys(row.CostPrices);
                        var cv = Object.values(row.CostPrices);
                        ck.forEach(function (a, b) {
                            if (a == unitId) {
                                costPrice = cv[b];
                            }
                        });
                        costAmount = parseFloat(parseFloat((row.Quantity == undefined || row.Quantity == "") ? "0" : row.Quantity) * parseFloat(tradePrice)).toFixed(2);
                        $("#InventoryProfitLossItems").bootstrapTable('updateRow', {
                            index: index,
                            row: {
                                UnitId: curUnit,
                                UnitName: $(this).find("option:selected").text(),
                                CostPrice: costPrice,
                                CostAmount: costAmount
                            }
                        });
                    });
                }

                //debug
                $.each($('#InventoryProfitLossItems').bootstrapTable('getData'), function (i, row) {
                    //console.log(row);
                });
            }
        });

        //表单检查
        var formValidator = function () {
            var isVaild = true;

            if ($("#ChargePerson").val() == "" || $("#ChargePerson").val() == "0") {
                $("#ChargePerson").focus();
                $(this).showToastr("请选择经办人！");
                return false;
            }
            else if ($("#WareHouseId").val() == "" || $("#WareHouseId").val() == "0") {
                $("#WareHouseId").focus();
                $(this).showToastr("请选择仓库！");
                return false;
            }
            else if ($("#datetimepicker_InventoryDate").val() == "") {
                $("#datetimepicker_InventoryDate").focus();
                $(this).showToastr("请选择盘点时间！");
                return false;
            }

            return isVaild;
        }

        //保存服务器
        var submitRemoteServer = function (showMessage, callback, loadingButton) {
            if (!formValidator()) {
                if (loadingButton != undefined) {
                    loadingButton.stop();
                }
                return false;
            };

            var flag = 0;
            var allTableData = $('#InventoryProfitLossItems').bootstrapTable('getData');
            var flag = 0;
            $.each(allTableData, function (i, row) {
                if (Object.keys(row).length > 1) {
                    if (row.ProductId == "" || row.Quantity == "")
                        flag++;
                }

            });

            if (flag == allTableData.length) {
                if (loadingButton != undefined) {
                    loadingButton.stop();
                }
                $(this).showToastr("请先完整填写单据明细!");
                return false;
            }

            var serializeData = [];
            var accountings = [];

            $.each(allTableData, function (i, row) {
                var tmpObj = {
                    Id: (!row.Id || typeof (row.Id) == 'undefined' || row.Id == "") ? "0" : row.Id,
                    ProductId: (!row.ProductId || typeof (row.ProductId) == 'undefined' || row.ProductId == "") ? "0" : row.ProductId,
                    UnitId: (!row.UnitId || typeof (row.UnitId) == 'undefined' || row.UnitId == "") ? "0" : row.UnitId,
                    Quantity: (!row.Quantity || typeof (row.Quantity) == 'undefined' || row.Quantity == "") ? "0" : row.Quantity,
                    CostPrice: (!row.CostPrice || typeof (row.CostPrice) == 'undefined' || row.CostPrice == "") ? "0" : row.CostPrice,
                    CostAmount: (!row.CostAmount || typeof (row.CostAmount) == 'undefined' || row.CostAmount == "") ? "0" : row.CostAmount,
                    StockQty: (!row.StockQty || typeof (row.StockQty) == 'undefined' || row.StockQty == "") ? "0" : row.StockQty
                };
                if (tmpObj.ProductId != 0)
                    serializeData.push(tmpObj);
            });

            //保存的数据
            var postData = {
                ChargePerson: $("#ChargePerson").val(),
                WareHouseId: $("#WareHouseId").val(),
                InventoryByMinUnit: $("#InventoryByMinUnit").val(),
                InventoryDate: $("#datetimepicker_InventoryDate").val(),
                Remark: $("#Remark").val(),
                Items: serializeData,
            };

            //保存服务器
            $(this).remoteSubmit("post", "/InventoryProfitLoss/CreateOrUpdate?billId=@(Model.Id)", postData, $("#InventoryProfitLossItems"), callback, showMessage, loadingButton);

        };

        //保存单据项目
        $("#FormSubmit").click(function () {
            //防止重复保存
            var loadingButton = Ladda.create(this);
            loadingButton.start();
            submitRemoteServer(null, function (data) {
                if (data.Success) {
                    window.location.href = "/InventoryProfitLoss/List";
                }
            }, loadingButton);
        });

        //保存并继续
        $("#FormSubmitContinue").click(function () {
            //防止重复保存
            var loadingButton = Ladda.create(this);
            loadingButton.start();
            submitRemoteServer(null, null, loadingButton);
        });


        //审核
        $("#btn_auditing").click(function () {
            //防止重复保存
            var loadingButton = Ladda.create(this);
            loadingButton.start();
            var returnUrl = "@Context.Request.Query["returnURL"]";
            if (returnUrl.indexOf("AuditedStatus=") == -1) {
                if (returnUrl.indexOf("?") == -1) {
                    returnUrl += "?";
                } else {
                    returnUrl += "&";
                }
                returnUrl += "AuditedStatus=False";
            } else {
                if (returnUrl.indexOf("AuditedStatus=False") == -1) {
                    returnUrl = returnUrl.replace("AuditedStatus=", "AuditedStatus=False");
                }
            }
            returnUrl = returnUrl.replace(/amp;/g, "");
            $(this).remoteSubmit("get", "/InventoryProfitLoss/Auditing", { id: "@Model.Id" }, null, function () { location.href = returnUrl; }, null, loadingButton);
        });

        //红冲
        $("#btn_reversed").click(function () {
            //防止重复保存
            var loadingButton = Ladda.create(this);

            WinMsg.confirm({ message: "确认要红冲吗？" }).on(function (e) {
                if (!e) {
                    return;
                }
                loadingButton.start();
                $(this).remoteSubmit("get", "/InventoryProfitLoss/Reverse", { id: "@Model.Id" }, null, function () { location.href = "/InventoryProfitLoss/List"; }, null, loadingButton);
            });
        });

        //打印
        $("#btn_print").click(function () {
            //防止重复保存
            var loadingButton = Ladda.create(this);
            WinMsg.confirm({ message: "确认要打印吗？" }).on(function (e) {
                if (!e) {
                    return;
                }
                loadingButton.start();
                var setting = {}
                var LODOP = null;
                var rs = $(this).getSyncData("/InventoryProfitLoss/PrintSetting", null, "get");
                if (rs.Success) {
                    setting = rs.Data;
                    loadCLodop(document, setting.PrintPort, () => {
                        //判断页面是否已加载LODOP
                        if (LODOP == null || LODOP == undefined) {
                            LODOP = getLodopFirst(document);
                            LODOP = getLodopSecond(document);
                        }
                        if (LODOP == null || LODOP == undefined) {
                            loadingButton.stop();
                            return;
                        } else {
                            $("#printData").html("");
                        }
                        $(this).remoteSubmit("get", "/InventoryProfitLoss/Print", { selectData: "@Model.Id" }, null, function (result) {
                            if (result.Success) {
                                var html = result.Data;
                                LODOP.PRINT_INIT("盘点盈亏单");
                                if (!isNaN(setting.PaperWidth) && !isNaN(setting.PaperHeight)) {
                                    LODOP.SET_PRINT_PAGESIZE(1, setting.PaperWidth + "mm", setting.PaperHeight + "mm");
                                }
                                if (setting.IsPrintPageNumber) {
                                    LODOP.ADD_PRINT_HTM(1, 600, 300, 100, "总页号：<font color='#0000ff' format='ChineseNum'><span tdata='pageNO'>第##页</span>/<span tdata='pageCount'>共##页</span></font>");
                                }
                                if (!isNaN(setting.MarginTop) && !isNaN(setting.MarginLeft) && !isNaN(setting.MarginRight) && !isNaN(setting.MarginBottom)) {
                                    LODOP.ADD_PRINT_HTM(setting.MarginTop + "mm", setting.MarginLeft + "mm", "RightMargin:" + setting.MarginRight + "mm", "BottomMargin:" + setting.MarginBottom + "mm", html);
                                }
                                LODOP.SET_PRINT_STYLEA(0, "Horient", 2);
                                LODOP.PREVIEW(true);
                                loadingButton.stop();
                            }
                            else {
                                $(this).showToastr(result.Message);
                            }
                        }, null, loadingButton);
                    });
                }

            });
        });
        //作废
        $('#btn_cancel').click(function () {
            //防止重复保存
            var loadingButton = Ladda.create(this);
            WinMsg.confirm({ message: "确认要作废吗？" }).on(function (e) {
                if (!e) {
                    return;
                }
                loadingButton.start();
                $(this).remoteSubmit("get", "/InventoryProfitLoss/Delete", { id: "@Model.Id" }, null, function () { location.href = "/InventoryProfitLoss/List"; }, null, loadingButton);
            });
        });
    });
</script>